package com.lcx.security.controller;

import com.lcx.security.FFmpegCommandManager.FFmpegManagerImpl;
import com.lcx.security.dao.TaskDaoImpl;
import com.lcx.security.entity.OutHandler;
import com.lcx.security.entity.TaskEntity;
import com.lcx.security.main.FFmpegCMD;
import com.lcx.security.service.impl.DefaultOutHandlerMethod;
import com.lcx.security.service.impl.TaskHandlerImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

@RestController
public class HelloController {
    protected static Logger logger = LoggerFactory.getLogger(HelloController.class);
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    @GetMapping("/stop")
    public String stop(){
        TaskDaoImpl taskDao = new TaskDaoImpl();
        TaskHandlerImpl taskHandler = new TaskHandlerImpl(new DefaultOutHandlerMethod());
        Collection<TaskEntity> taskDaoAll = taskDao.getAll();
        Iterator<TaskEntity> iter = taskDaoAll.iterator();
        TaskEntity tasker = null;

        while (iter.hasNext()) {
            tasker = iter.next();
            taskHandler.stop(tasker);
            taskDao.remove(tasker.getId());
            logger.info(tasker.toString());

        }

        return "暂停成功";
    }

    @GetMapping("/test2")
    public String testshell12(@RequestParam(value = "num",defaultValue = "2") Integer num){
        String shell = "";
        if (num % 2 ==0){
            shell = "ffmpeg  -rtsp_transport tcp -re -i rtsp://macao:GREEmc2022@131.255.83.58:554/h264/ch1/sub/av_stream -vcodec copy -preset ultrafast -acodec copy -f flv rtmp://127.0.0.1:8899/hls";
        }else{
            shell = "/usr/local/ffmpeg/bin/ffmpeg  -rtsp_transport tcp -re -i rtsp://macao:GREEmc2022@131.255.83.58:554/h264/ch1/sub/av_stream -vcodec copy -preset ultrafast -acodec copy -f flv rtmp://127.0.0.1:8899/hls";
        }
        Process process = null;
        OutHandler outHandler = null;
        OutHandler outHandler1 = null;
        DefaultOutHandlerMethod outHandlerMethod = new DefaultOutHandlerMethod();
        try {
            logger.info("准备执行:"+shell);
            String[] cmds = {"/bin/sh","-c",shell};//linux
            process = Runtime.getRuntime().exec(cmds);//linux

            logger.info("执行完毕"+shell);
            outHandler = new OutHandler(process.getInputStream(),"test1.txt",outHandlerMethod);
            outHandler.start();
            outHandler1 = new OutHandler(process.getErrorStream(),"test1.txt",outHandlerMethod);
            outHandler1.start();
            Thread.sleep(50000);
            logger.info("马上关闭"+shell);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }finally {

            assert outHandler != null;
            outHandler.destroy();
            assert outHandler1 != null;
            outHandler1.destroy();
            process.destroy();
            logger.info("已关闭"+shell);
        }
        return "已执行";
    }

    @GetMapping("/test1")
    public String testshell1(@RequestParam(value = "num",defaultValue = "2") Integer num){
        String shell = "";
        if (num % 2 !=0){
            shell = "ffmpeg  -rtsp_transport tcp -re -i rtsp://macao:GREEmc2022@131.255.83.58:554/h264/ch1/sub/av_stream -vcodec copy -preset ultrafast -acodec copy -f flv rtmp://127.0.0.1:8899/hls";
        }else{
            shell = "/usr/local/ffmpeg/bin/ffmpeg  -rtsp_transport tcp -re -i rtsp://macao:GREEmc2022@131.255.83.58:554/h264/ch1/sub/av_stream -vcodec copy -preset ultrafast -acodec copy -f flv rtmp://127.0.0.1:8899/hls";
        }
        Process process = null;
        OutHandler outHandler = null;
        OutHandler outHandler1 = null;
        DefaultOutHandlerMethod outHandlerMethod = new DefaultOutHandlerMethod();
        try {
            logger.info("准备执行:"+shell);
            String[] cmds = {"sh","-c",shell};//linux
            process = Runtime.getRuntime().exec(cmds);//linux
            logger.info("执行完毕"+shell);
            outHandler = new OutHandler(process.getInputStream(),"test1.txt",outHandlerMethod);
            outHandler.start();
            outHandler1 = new OutHandler(process.getErrorStream(),"test1.txt",outHandlerMethod);
            outHandler1.start();
            Thread.sleep(50000);
            logger.info("马上关闭"+shell);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }finally {

            assert outHandler != null;
            outHandler.destroy();
            assert outHandler1 != null;
            outHandler1.destroy();
            process.destroy();
            logger.info("已关闭"+shell);
        }
        return "已执行";
    }

    @GetMapping("/test3")
    public String test3(@RequestParam(value = "num",defaultValue = "2") Integer num){
        String shell = "";
        if (num % 2 ==0){
            shell = "ffmpeg  -rtsp_transport tcp -re -i rtsp://macao:GREEmc2022@131.255.83.58:554/h264/ch1/sub/av_stream -vcodec copy -preset ultrafast -acodec copy -f flv rtmp://127.0.0.1:8899/hls";
        }else{
            shell = "/usr/local/ffmpeg/bin/ffmpeg  -rtsp_transport tcp -re -i rtsp://macao:GREEmc2022@131.255.83.58:554/h264/ch1/sub/av_stream -vcodec copy -preset ultrafast -acodec copy -f flv rtmp://127.0.0.1:8899/hls";
        }
        Process process = null;
        OutHandler outHandler = null;
        OutHandler outHandler1 = null;
        DefaultOutHandlerMethod outHandlerMethod = new DefaultOutHandlerMethod();
        try {
            logger.info("准备执行:"+shell);
            process = Runtime.getRuntime().exec(shell);
            logger.info("执行完毕"+shell);
            outHandler = new OutHandler(process.getInputStream(),"test1.txt",outHandlerMethod);
            outHandler.start();
            outHandler1 = new OutHandler(process.getErrorStream(),"test1.txt",outHandlerMethod);
            outHandler1.start();
            Thread.sleep(50000);
            logger.info("马上关闭"+shell);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }finally {

            assert outHandler != null;
            outHandler.destroy();
            assert outHandler1 != null;
            outHandler1.destroy();
            process.destroy();
            logger.info("已关闭"+shell);
        }
        return "已执行";
    }



    @GetMapping("/queryall")
    public String queryAll(){
        TaskDaoImpl taskDao = new TaskDaoImpl();
        Collection<TaskEntity> taskDaoAll = taskDao.getAll();
        logger.info("当前有几个推流任务"+taskDaoAll.size());
        return taskDaoAll.toString();
    }

}


